const jwt = require('koa-jwt')
const Router = require('koa-router')
const router = new Router({ prefix: '/users' })
// 导入控制器
const {
  find, findById, create, update, delete:del,
  login, checkOwner, listFollowing, follow,
  checkUserExist ,unFollow, listFollowers,
  listFollowingTopics,followTopic, unFollowTopic,
  listQuestions,
  listLikingAnswers, likeAnswer, unlikeAnswer,
  listDislikingAnswers, dislikeAnswer,unDislikeAnswer,
  listCollectingAnswers, collectAnswer, unCollectAnswer

} = require('../controllers/users')
// 引入密钥
const { secret } = require('../config')

// 认证中间件
const auth = jwt({ secret })
// 话题是否存在
const { checkTopicExist } = require('../controllers/topics')
// 答案是否存在
const { checkAnswerExist } = require('../controllers/answers')

router.get('/', find)
router.post('/', create)
router.get('/:id', findById)
// 先认证再授权
router.patch('/:id', auth, checkOwner, update)
router.delete('/:id', auth, checkOwner, del)
router.post('/login', login)

// 获取特定用户关注人列表
router.get('/:id/following', listFollowing)
// 获取特定用户粉丝列表
router.get('/:id/followers', listFollowers)
// 关注用户
// 先认证再判断用户是否存在
router.put('/following/:id', auth, checkUserExist, follow)
// 取消关注用户
router.delete('/following/:id', auth, checkUserExist, unFollow)

// 获取特定用户关注话题列表
router.get('/:id/followingTopics', listFollowingTopics)
// 关注话题
router.put('/followingTopics/:id', auth, checkTopicExist, followTopic)
// 取消关注话题
router.delete('/followingTopics/:id', auth, checkTopicExist, unFollowTopic)

// 获取特定用户问题列表
router.get('/:id/questions', listQuestions)

// 获取特定用户赞的答案列表
router.get('/:id/likingAnswers', listLikingAnswers)
// 赞答案 赞的时候取消踩
router.put('/likingAnswers/:id', auth, checkAnswerExist, likeAnswer, unDislikeAnswer)
// 取消赞答案
router.delete('/likingAnswers/:id', auth, checkAnswerExist, unlikeAnswer)

// 获取特定用户踩的答案列表
router.get('/:id/dislikingAnswers', listDislikingAnswers)
// 踩答案 踩的时候取消赞
router.put('/dislikingAnswers/:id', auth, checkAnswerExist, dislikeAnswer, unlikeAnswer)
// 取消踩答案
router.delete('/dislikingAnswers/:id', auth, checkAnswerExist, unDislikeAnswer)

// 获取特定用户收藏的答案列表
router.get('/:id/collectingAnswers', listCollectingAnswers)
// 收藏答案
router.put('/collectingAnswers/:id', auth, checkAnswerExist, collectAnswer)
// 取消收藏答案
router.delete('/collectingAnswers/:id', auth, checkAnswerExist, unCollectAnswer)

module.exports = router


